define(['common/js/service/classtype-service.js',
        'common/js/service/business-service.js',
        'common/js/directive/ng-file-upload.js',
        'common/lib/jQuery-photoClip-master/js/hammer.js',
        'common/lib/jQuery-photoClip-master/js/iscroll-zoom.js',
        'common/lib/jQuery-photoClip-master/js/lrz.all.bundle.js',
        'common/lib/jQuery-photoClip-master/js/jquery.photoClip.js',
        'common/js/service/boss-storage-service-0.1'],
    function () {
        controllerModule.controller("BusinessController", function ($scope, ClassTypeService,BusinessService,cookieDataStorage,Upload,dataStorage, MaiDialog) {
        	$scope.columnDefs=[{
    			field: 'businessId',
    			width: '5%',
    			displayName: 'ID'
    		},{
    			field: 'businessName',
    			width: '15%',
    			displayName: '商家名称'
    		},{
    			field: 'city',
    			width: '10%',
    			displayName: '城市/联系方式',
    			cellTemplate:function(entity){
    				return "<span>"+entity.city+"<br>"+entity.telphone+"</span>";
    			}
    		},{
    			field: 'outletName',
    			width: '10%',
    			displayName: '门店名称'
    		},{
    			field: 'outletLevel',
    			width: '7%',
    			displayName: '显示顺序'
    		},{
    			field: 'visitCount',
    			width: '10%',
    			displayName: '浏览/联系次数',
    			cellTemplate: function(entity){
    				return "<span>"+entity.visitCount+ "/" +entity.contactCount+"</span>";
    			}
    		},{
    			field: 'outletDesc',
    			width: '25%',
    			displayName: '关键字'
    				
    		},{
    			field: 'operate',
    			width: '15%',
    			displayName: '操作',
                cellTemplate: function(entity){
    				var menu = "<a class='btn btn-primary' ng-click='openEdit(entity)'>编辑</a>" +
    						"&nbsp;&nbsp;&nbsp;&nbsp;<a class='btn btn-primary' ng-click='deleteById(entity)'>删除</a>";
    				return menu;
    			}
    		}];
        	
        	$scope.deleteById = function(entity){
        		MaiDialog.confirm("确认删除商家吗？",function(data){
        			if(data){
        				BusinessService.deleteById({"businessId":entity.businessId}).then(function(data){
                			if(data.code == 200){
                				window.refresh();
                			}else{
                				MaiDialog.alert("删除失败");
                			}
                		});
        			}
        		});
        	};
        	
        	$scope.openAdd = function(){
        		$("#addDialog").modal("show");
        	};
        	
        	window.refresh = function(){
        		$scope.$broadcast('reloadGrid');
        	};
        	
        	$scope.openEdit = function(entity){
        		$("#editDialog").modal("show");
        		$("#edit_id").val(entity.businessId);
            	$scope.$broadcast("business-editEcho",entity);
        	}
        });
        
        
        controllerModule.controller("BusinessAddController", function ($scope, ClassTypeService, BusinessService,cookieDataStorage, Upload,dataStorage, MaiDialog) {
        	var $styleCss = 'background-color: rgb(102, 102, 102); background-size: contain; background-position: 50% 50%; background-repeat: no-repeat;';
    		console.log("BusinessAddController");
        	$scope.selectSaveImage = function(){
    			$("#myImageFile1").click();
    		};
    		
    		/**
    		 * 保存项目图片
    		 */
    		$scope.saveProjectImage = function($files){
    			if($files && $files[0]){
    				if("image/jpg,image/jpeg,image/png".indexOf($files[0].type) == -1){ //
    					MaiDialog.alert("只允许上传PNG、JPG或JPEG格式的图片");
    					return;
    				}
    				//文件太大
    				if(parseInt($files[0].size) > 102400){
    					MaiDialog.alert("图片不能大于100KB");
    					return;
    				}
    		        //清除掉提示信息
    				$("#add_warn_tip").text("");
    				$scope.upload = Upload.upload({
    			        url: window.rootPath+'/api/business/upload', //upload.php script, node.js route, or servlet url
    			        method:'POST',
    			        file: $files[0]
    				}).progress(function(evt) {
    					console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total));
    				}).success(function(data, status, headers, config) {        // file is uploaded successfully
    			    	if(data){
    			    		if(data.code == 200){
    			    			if(data.data){
    			    				var uploadImage = data.data;
    			    				uploadImage.image = uploadImage.image.replace(/[\r\n]/g,"");
    			    				$scope.uploadImage = uploadImage; //将图片信息放在scope中
    			    				
    			    				var temp = uploadImage.image;
    			    				if(temp.toString().indexOf("data:image/jpg;base64,") == -1){
    			    					temp = "data:image/jpg;base64,"+temp;
    								}
    		    					$("#project_image_add").attr("filePath",uploadImage.filePath);
    			    				$("#project_image_add").attr("src",temp);
    			    				$("#project_image_add").attr("style","display:block");
    			    			}else{
    			    				MaiDialog.alert("上传失败,请上传正确的图片");
    			    			}
    			    		}else{
    			    			MaiDialog.alert(data.msg);
    			    		}
    			    	}
    				});
    			}
    		}
        	
        	$scope.save = function(param){
        		var businessParam = {
    				"businessName":param.businessName,
    				"city":param.city,
    				"remark":param.remark,
    				"telphone":param.telphone,
    				"outletName":param.outletName,
    				"outletAddress":param.outletAddress,
    				"outletPhone":param.outletPhone,
    				"outletLevel":param.outletLevel,
    				"outletDesc":param.outletDesc,
    				"businessImage":$("#project_image_add").attr("filePath"),
    				"imageUrl1":param.imageUrl1,
    				"imageUrl2":param.imageUrl2,
    				"imageUrl3":param.imageUrl3,
    				"imageUrl4":param.imageUrl4,
    				"imageUrl5":param.imageUrl5,
    				"outletDetail":param.outletDetail
        		};
        		BusinessService.save(businessParam).then(function(data){
        			if(data.code == 200){
        				$("#addDialog").modal("hide");
        				window.refresh();
        			}else{
        				MaiDialog.alert("保存失败");
        			}
        		});
        	}
        });
        
        controllerModule.controller("BusinessEditController", function ($scope, ClassTypeService, BusinessService,cookieDataStorage, Upload,dataStorage, MaiDialog) {
        	$scope.selectYourImage = function(){
    			$("#myImageFile2").click();
    		}
        	
        	$scope.selectBusinessImage = function(id){
    			$("#"+id).click();
    		}
        	
    		/**
    		 * 更新项目图片
    		 */
    		$scope.updateProjectImage = function($files){
    			if($files && $files[0]){
    				if("image/jpg,image/jpeg,image/png".indexOf($files[0].type) == -1){
    					MaiDialog.alert("只允许上传PNG、JPG或JPEG格式的图片");
    					return;
    				}
    				//文件太大
    				if(parseInt($files[0].size) > 102400){
    					MaiDialog.alert("您选择的图片大于100KB");
    					return;
    				}
    				$scope.upload = Upload.upload({
    			        url: window.rootPath+'/api/business/upload', //upload.php script, node.js route, or servlet url
    			        method:'POST',
    			        file: $files[0]
    				}).progress(function(evt) {
    					console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total));
    				}).success(function(data, status, headers, config) {        // file is uploaded successfully
    			    	if(data){
    			    		if(data.code == 200){
    			    			$scope.updateImageOk = true;
    			    			if(data.data){
    			    				var uploadImage = data.data;
    			    				uploadImage.image = uploadImage.image.replace(/[\r\n]/g,"");
    			    				$scope.uploadImage = uploadImage; //将图片信息放在scope中
    			    				
    			    				var temp = uploadImage.image;
    			    				if(temp.toString().indexOf("data:image/jpg;base64,") == -1){
    			    					temp = "data:image/jpg;base64,"+temp;
    								}
    		    					$("#project_image_update").attr("filePath",uploadImage.filePath);
    			    				$("#project_image_update").attr("src",temp);
    			    				$("#project_image_update").attr("style","display:block");
    			    				$("#update_warn_tip").text("");
    			    			}else{
    			    				MaiDialog.alert("上传失败,请上传正确的图片");
    			    			}
    			    		}else{
    			    			MaiDialog.alert(data.msg);
    			    		}
    			    	}
    				});
    			}
    		}
    		
    		/**
    		 * 更新项目图片
    		 */
    		$scope.updateOtherImage = function($files,index){
    			var businessId = $("#edit_id").val();
    			if($files && $files[0]){
    				if("image/png".indexOf($files[0].type) == -1){
    					MaiDialog.alert("只允许上传PNG图片");
    					return;
    				}
    				$scope.upload = Upload.upload({
    			        url: window.rootPath+'/api/business/uploadImages?businessId='+businessId+'&index='+index, //upload.php script, node.js route, or servlet url
    			        method:'POST',
    			        file: $files[0]
    				}).progress(function(evt) {
    					console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total));
    				}).success(function(data, status, headers, config) {        // file is uploaded successfully
    			    	if(data){
    			    		if(data.code == 200){
    			    			$scope.updateImageOk = true;
    			    			if(data.data){
    			    				$("#project_image"+index+"_src").attr("src", "/upload/images/"+data.data.filePath+"?t="+Math.random()); 
    			    				MaiDialog.alert(data.msg);
    			    			}else{
    			    				MaiDialog.alert("上传失败,请上传正确的图片");
    			    			}
    			    		}else{
    			    			MaiDialog.alert(data.msg);
    			    		}
    			    	}
    				});
    			}
    		}
    		
    		
    		/**
    		 * 清空上一次缓存的图片
    		 */
    		function clearFiles(){
    			var myFile = $("#myImageFile2") ;
    			if(myFile){
    				myFile.after(myFile.clone().val(""));      
        			myFile.remove();
    			}
    		}
        	
        	$scope.save = function(param){
        		var businessParam = {
        			"businessId":$("#edit_id").val(),
    				"businessName":param.businessName,
    				"businessImage":$('#project_image_update').attr("filePath"),
    				"city":param.city,
    				"remark":param.remark,
    				"telphone":param.telphone,
    				"outletName":param.outletName,
    				"outletAddress":param.outletAddress,
    				"outletPhone":param.outletPhone,
    				"outletLevel":param.outletLevel,
    				"outletDesc":param.outletDesc,
//    				"imageUrl1":param.imageUrl1,
//    				"imageUrl2":param.imageUrl2,
//    				"imageUrl3":param.imageUrl3,
//    				"imageUrl4":param.imageUrl4,
//    				"imageUrl5":param.imageUrl5,
    				"outletDetail":param.outletDetail
            	};
        		BusinessService.save(businessParam).then(function(data){
        			if(data.code == 200){
        				$("#editDialog").modal("hide");
        				window.refresh();
        			}else{
        				MaiDialog.alert("保存失败");
        			}
        		});
        	}
        	
        	$scope.$on("business-editEcho",function(event,entity){
    			$("#projectUpdateModal").modal("show");$("#update_warn_tip").text("");
    			$("#edit_id").val(entity.businessId);
    			if(entity.businessImageByte){
    				if(entity.businessImageByte.toString().indexOf("data:image/jpg;base64,") == -1){
    					entity.businessImageByte = "data:image/jpg;base64,"+entity.businessImageByte;
    				}
    				$(".view_edit_img").attr("filePath",entity.projectImageUrl);
    				$(".view_edit_img").attr("src",entity.businessImageByte);
    			}else{
    				entity.businessImageByte = "";
    			}
    			$scope.businessId = $("#edit_id").val();
    			$("#projectUpdateModal").modal("show");$("#update_warn_tip").text("");
    			$("#project_image_update").attr("filePath",entity.businessImage);
    			$("#project_image_update").attr("src",entity.businessImageByte);
    			$("#project_image_update").attr("style","display:block");
    			$scope.$broadcast('echoForm-editForm',entity);
    		});
        });
    });